package com.yubest.demo.controller;

import com.yubest.demo.anno.Log;
import com.yubest.demo.anno.Login;
import com.yubest.demo.anno.Repeat;
import com.yubest.demo.dto.DemoReqDTO;
import com.yubest.demo.dto.LoginBean;
import com.yubest.demo.dto.Response;
import com.yubest.demo.util.CacheUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.UUID;

/**
 * @Author hweiyu
 * @Description
 * @Date 2021/3/1 14:01
 */
@RestController
public class DemoController {

    /**
     * 日志测试
     * @param username
     * @param password
     * @return
     */
    @Log
    @PostMapping(value = "/login")
    public Response<String> login(@RequestParam("username") String username, @RequestParam("password") String password) {
        String token = UUID.randomUUID().toString();
        CacheUtil.set(token, new LoginBean()
                .setUsername(username)
                .setPhone("18888888888")
                .setUserId(1L), 3600L);
        return Response.success(token);
    }

    /**
     * 日志，登陆测试
     * @param request
     * @return
     */
    @Log
    @Login
    @GetMapping(value = "/info")
    public Response<LoginBean> info(HttpServletRequest request) {
        LoginBean login = CacheUtil.get(request.getHeader("access-token"), LoginBean.class);
        return Response.success(login);
    }

    /**
     * 日志，重复提交测试
     * 以com.yubest.demo.dto.DemoReqDTO中的a属性作为重复提交的标识
     * @param reqDTO
     * @return
     */
    @Log
    @Repeat(key = "#reqDTO.a", expire = 10)
    @PostMapping(value = "/checkRepeat")
    public Response<Void> checkRepeat(@RequestBody DemoReqDTO reqDTO) {
        return Response.success();
    }

}
